<!DOCTYPE html>
<html lang="zh">
    <head>
        <meta charset="utf-8" />
		<meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>新增/修改流程节点配置</title>
        <style>
           div,th,td {
                font-size: 12px;
           }
           td a {
               margin: 0px 2px;
           }
        </style>
    </head>
    <body>
        <div id="content">
            <div id="currentUser" style="color:green;font-weight:bold;font-size:14px;"></div>
            <div><a id="backTo" href='#'>返回</a></div>

            <div>&nbsp;</div>
            <div id="info">新增/修改流程节点配置</div>

            <div>&nbsp;</div>
            <div>
                <form id="flowForm">
                    
                    工单号(order_id)<font color="red">*</font>: <span id="orderId"></span><br/>
                    工单任务代理人的版本号(assignee_version)<font color="red">*</font>: <input type="text" id="assigneeVersion" value="1"><span style='color:green;font-weight:bold;'>版本号默认为1就可，一般不用修改。</span><br>
                    工单任务代理人的用户ID(assignee)<font color="red">*</font>: 
                    <select class="selectUser" id="assignee" name="assignee">
                        <option value ="zhaoyl">项目经理（zhaoyl）</option>
                        <option value ="linjh">部门经理（linjh）</option>
                        <option value ="xuw">总经理（xuw）</option>
                    </select>
                    <span style='color:green;font-weight:bold;'>这里为了演示，写死了几个用户，正式环境改成读用户体系取就可。</span><br>
                    
                    工单任务代理人的顺序(assignee_order)<font color="red">*</font>: <input type="text" id="assigneeOrder" value="1">工单任务代理人的顺序，从1开始，用于表示此工单任务代理人为第几个审批人/处理人<br>
                    任务(节点)名(task_name): <input type="text" id="taskName" value="审批"><br>
                    流程执行到此任务(节点)时的工单状态: <input type="text" id="orderStatus" value="1"><span style='color:green;font-weight:bold;'>默认使用1即可，一般不需要修改。</span>流程执行到此任务(节点)时的工单状态，参考order_status表(order_status)<br>
                    此工单任务代理人是否可以取消工单(can_cancel_order): <input type="text" id="canCancelOrder" value="false"><span style='color:green;font-weight:bold;'>默认使用false即可，一般不需要修改。</span><br>
                    备注(remark): <input type="text" id="remark"><br>
                    <input type="button" onclick="addOrEdit();" value="保存">
                </form>
            </div>

            <div>&nbsp;</div>
            <div>-----工单状态(order_status)	的说明-----</div>
            <div>
               WAIT_FOR_VERIFY("待审批", "1"),<br>
               FINISHED("已归档", "3"),<br>
               WAIT_FOR_UPDATE("待修改", "4"),<br>
               INVALIDED("已作废", "5"),<br>
               CANCELED("已撤单", "6"),<br>
            </div>

        </div>
        <script src="js/config.js"></script>
        <script src="js/jquery.min.js"></script>
        <script type="text/javascript">
            var contextPath = getContextPath();
            var id = getQueryVariable("id");
            var userId = getQueryVariable("userId");
            var orderType = getQueryVariable("orderType");
            var orderId = getQueryVariable("orderId");
            var assigneeVersion = getQueryVariable("assigneeVersion");
            var assignee = getQueryVariable("assignee");

            //返回按钮
            document.getElementById("backTo").href = contextPath + "/flowNodeList.html?userId="+userId+"&orderType="+orderType;

            $(function() {
                //模拟所有参与人员
                //lix：普通人员
                //zhaoyl：项目经理
                //liujh,linjh,huangjc：部门经理
                //xuw：总经理
                let arr = ['lix', 'zhaoyl', 'liujh', 'linjh', 'huangjc', 'xuw'];
                if(!arr.includes(userId)){
                    alert("未合法的userId，请查看main.html里面的定义.");
                    return;
                }
                //设置当前用户信息：
                document.getElementById("currentUser").innerHTML = "你是："+userId;
                document.getElementById("orderId").innerText = "" + orderType;

                //如果id不为空，则加载数据库中的数据进行填充表单
                if(id!=""){
                    $.ajax({
                        url:  window.location.origin + contextPath + '/workOrderAssignee/selectByPrimaryKey',
                        data: JSON.stringify({
                            orderId: orderId,
                            assigneeVersion: assigneeVersion,
                            assignee: assignee
                        }),
                        type: 'post',
                        contentType: 'application/json;charset=utf-8',
                        dataType: "json",
                        success: function (res) {
                            if(res){
                                var aRow = res;
                                document.getElementById("orderId").innerText = aRow.orderId;
                                document.getElementById("assigneeVersion").value = aRow.assigneeVersion;
                                //document.getElementById("assignee").value = aRow.assignee;
                                set_select_checked("assignee",aRow.assignee);
                                document.getElementById("assigneeOrder").value = aRow.assigneeOrder;
                                document.getElementById("taskName").value = aRow.taskName;
                                document.getElementById("orderStatus").value = aRow.orderStatus;
                                document.getElementById("canCancelOrder").value = aRow.canCancelOrder;
                                document.getElementById("remark").value = aRow.remark;
                            }
                        },
                        error: function (result) {alert("流程节点配置加载失败，内部错误.");}
                    });   
                }

            });

            //addOrEdit
            function addOrEdit(){

                var orderId = document.getElementById("orderId").innerText;
                var assigneeVersion = document.getElementById("assigneeVersion").value;
                
                //assignee
                //var assignee = document.getElementById("assignee").value;
                var obj=document.getElementById('assignee');
                var index=obj.selectedIndex; //序号，取当前选中选项的序号
                var assignee = obj.options[index].value;

                var assigneeOrder = document.getElementById("assigneeOrder").value;
                var taskName = document.getElementById("taskName").value;
                var orderStatus = document.getElementById("orderStatus").value;
                var canCancelOrder = document.getElementById("canCancelOrder").value;
                var remark = document.getElementById("remark").value;

                if(orderId === ""){
                    alert("工单号(order_id)不能为空.");
                    return;
                }

                if(assigneeVersion === ""){
                    alert("工单任务代理人的版本号(assignee_version).");
                    return;
                }
                if(assignee === ""){
                    alert("工单任务代理人的用户ID(assignee).");
                    return;
                }
                if(assigneeOrder === ""){
                    alert("工单任务代理人的顺序(assignee_order)不能为空.");
                    return;
                }

                if(id!=""){

                    //修改
                    $.ajax({
                        url:  window.location.origin + contextPath + '/workOrderAssignee/updateByPrimaryKey',
                        data: JSON.stringify({
                            id: id,                            
                            orderId: orderId,
                            assigneeVersion: assigneeVersion,
                            assignee: assignee,
                            assigneeOrder: assigneeOrder,
                            taskName: taskName,
                            orderStatus: orderStatus,
                            canCancelOrder: canCancelOrder,
                            remark: remark
                        }),
                        type: 'post',
                        contentType: 'application/json;charset=utf-8',
                        dataType: "json",
                        success: function (res) {
                            if(res){
                                alert("流程节点配置修改成功");
                                window.location.href = contextPath + "/flowNodeList.html?userId="+userId+"&orderType="+orderType;
                            }
                        },
                        error: function (result) {alert("流程节点配置失败，内部错误.");}
                    });  
                                     
                }else{
                    //新增
                    $.ajax({
                        url:  window.location.origin + contextPath + '/workOrderAssignee/insert',
                        data: JSON.stringify({
                            orderId: orderId,
                            assigneeVersion: assigneeVersion,
                            assignee: assignee,
                            assigneeOrder: assigneeOrder,
                            taskName: taskName,
                            orderStatus: orderStatus,
                            canCancelOrder: canCancelOrder,
                            remark: remark
                        }),
                        type: 'post',
                        contentType: 'application/json;charset=utf-8',
                        dataType: "json",
                        success: function (res) {
                            if(res){
                                alert("流程节点配置新增成功");
                                window.location.href = contextPath + "/flowNodeList.html?userId="+userId+"&orderType="+orderType;
                            }
                        },
                        error: function (result) {alert("流程节点配置失败，内部错误.");}
                    });   
                }
            }

            //获取浏览器地址上的url参数
            function getQueryVariable(variable){
                var query = window.location.search.substring(1);
                var vars = query.split("&");
                for (var i=0;i<vars.length;i++) {
                    var pair = vars[i].split("=");
                    if(pair[0] == variable){return pair[1];}
                }
                return "";
            }

            //设置select选中
            function set_select_checked(selectId, checkValue){
                var select = document.getElementById(selectId);
                for (var i = 0; i < select.options.length; i++){
                    if (select.options[i].value == checkValue){
                        select.options[i].selected = true;
                        break;
                    }
                }
            }

        </script>
    </body>
</html>